<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
use Think\Image;
class PicController extends BaseController {

    
	public function PicList(){
		     $pic =M('pic');
       
       //获得查询条件
       $search=$_REQUEST["search"]?$_REQUEST["search"]:"";  
       
       $starttime=$_REQUEST["starttime"]?$_REQUEST["starttime"]:"";
       $endtime=$_REQUEST["endtime"]?$_REQUEST["endtime"]:"";
       
       $status=0;

       
       $column=$_REQUEST["column"]?$_REQUEST["column"]:"";
       

       
       if($search){
       		$where["name"]=array("like","%$search%");
       		$where["des"]=array("like","%$search%");
       		
			$where['_logic'] = 'or';
			$condition['_complex'] = $where;
       }
       
       if($starttime){
       		$condition["created_time"]=array("EGT","$starttime");
       }
	   if($endtime){
	   		$condition["created_time"]=array("ELT","$endtime");
	   }
//	   if($status){
	   		$condition["pic_status"]=array("eq","$status");
//	   }

	   if($column){
	   		if($column==0){
	   			$condition["pic_column"]=array("in","3,4,5,6,7,8,9,10,11");
	   		}elseif($column==1){
	   			$condition["pic_column"]=array("in","3,4,5,6,7");
	   		}elseif($column==2){
	   			$condition["pic_column"]=array("in","8,9,10,11");
	   		}else{
	   			$condition["pic_column"]=array("eq","$column");
	   		}
	   	}
	   
	    import('ORG.Util.Page');// 导入分页类
		$pre=10;//每页多少条
	    $count      = $pic->where($condition)->count();// 查询满足要求的总记录数 $map表示查询条件
	    $Page       = new \Think\Page($count,$pre);// 实例化分页类 传入总记录数
	    
    	// 获取查询参数
   if($search){
    	$map['search'] = $search;
    	}
	    
    if($starttime){
    	$map['starttime'] = $starttime;
    	}
	    
    if($endtime){
    	$map['endtime'] = $endtime;
    	}
	    
    if($status){
    	$map['pic_status'] = $status;
    	}
	   
    if($column){
    	$map['pic_column'] = $column;
    	}
	    
	    
	    foreach($map as $key=>$val) {   
	    	$Page->parameter[$key]   =   urlencode($val); 
	    }

	    
	    $show       = $Page->show();// 分页显示输出
//	    var_dump($Page);exit;
	    // 进行分页数据查询
	    $pic_list = $pic->where($condition)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

	   
	    $this->assign('page',$show);// 赋值分页输出


	   $this->assign('search', $search);
	   $this->assign('starttime', $starttime);
	   $this->assign('endtime', $endtime);
	   $this->assign('status', $status);
	   $this->assign('column', $column);
	   
    	

		   
		   $this->assign('pic_list', $pic_list);

		   $this->display("pic_list");
    }
    
	public function PicAdd(){
		
		$this->display("pic_add");
    }
    
	public function upload(){
		
		$column=$_POST["column"];
		$des=$_POST["pic_des"];
		$name=$_POST["pic_name"];
		
		
		//保存表单数据 包括附件数据
		$pic = M("pic"); // 实例化User对象
		$data=$pic->create(); // 创建数据对象
	 	import('ORG.Net.UploadFile');
	 	
		$upload = new \Think\Upload();// 实例化上传类
		
		$upload->maxSize  = 13145728 ;// 设置附件上传大小13M
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//设置附件上传类型
		$upload->rootPath = './Public/upload/image';
		
		$savepath="mszy";
		$upload->savePath =  "/$savepath/";// 设置附件上传目录
		
		$upload->autoSub = false;
		
		
		
		
		
		
		//$upload->subType = 'date';  //子目录创建方式，默认为hash，可以设置为hash或者date 
        //$upload->dateFormat = 'Ym';
//        import('Think.Image');
//		$upload->thumbRemoveOrigin = true;
//		//设置需要生成缩略图，仅对图像文件有效 
//        $upload->thumb = true;
//        //设置需要生成缩略图的文件后缀 
//        $upload->thumbPrefix = 'm_,i_';//生产4张缩略图 
//        //设置缩略图最大宽度 
//        $upload->thumbMaxWidth = '400,600';
//        //设置缩略图最大高度 
//        $upload->thumbMaxHeight = '400,420';    
//        $upload->thumbPath = './Public/upload/image/mszythumb/';
		
		
		$info   =   $upload->upload();
		
		
		$image = new \Think\Image(); 
		$image->open('./Public/upload/image/mszy/'.$info['file']['savename']);
		// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
		$image->thumb(150,150)->save('./Public/upload/image/mszythumb/'.$info['file']['savename']);
		
		
//		var_dump($upload);exit;
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	        //$this->success('上传成功！');
	        
	        //修改数据库
	        $data["pic_name"] = $info['file']['savename'];
	    	
	        
	        //是否有缩略图	
			//if(file_exists("./Public/Uploads/m_".$date['F_FilePath'])){
			//	$date["F_FileIsMin"] = 1;
			//}
			
	        $data["created_time"] = Date("Y-m-d H:i:s");
	        $data["pic_column"] = $column;
	        $data["des"] = $des;
	        $data["name"] = $name;
	        
	        
	        $result=$pic->add($data);
	        
			if($result !== false){
		        $this->success('图片上传成功！', U('Admin/Pic/PicList', '', false, true));
		    }else{
		        $this->error("图片上传失败");
		    }
	    }
	 	
    }
    
	public function PicEdit(){
		$id=$_GET["id"];
		$pic = M('pic')->where("id=$id")->find();
		
		$this->assign('pic', $pic);
		$this->display("pic_edit");
    }
    
    
	public function Edit(){
		$data["pic_column"]=$_POST["column"];
		$data["des"]=$_POST["pic_des"];
		$data["name"]=$_POST["pic_name"];
		$id=$_POST["id"];
		$result = M('pic')->where("id=$id")->save($data);
		
		$this->assign('pic', $pic);
		if($result){
		$this->success("修改图片成功", U('Admin/Pic/PicList', '', false, true));
		}else{
		$this->success("修改图片失败", U('Admin/Pic/PicList', '', false, true));
		}
		
    }
    
 	public function Delete(){
    	
    	if($_GET["radio_del"]!=""){
    		$id=$_GET["radio_del"];
    		$pic = M('pic');
    		$data["pic_status"]=1;
	    	$result=$pic->where("id=$id")->save($data);
	    	
    		if($result !== false){
				 $this->success("图片删除成功 ！", U('Admin/Pic/PicList', '', false, true));
			}else{
				 $this->success('没有勾选删除图片！', U('Admin/Pic/PicList', '', false, true));
			}
    	}else{
	    	if($_GET["hidden_delete"]!=""){
	    		$arr_delete=$_GET["hidden_delete"];

	    		$arr_delete=substr($arr_delete,0,strlen($arr_delete)-1); 
	    		$arr_delete=explode("_",$arr_delete);
	    		
	    		$n=0;
	    		$pic = M('pic');
	    		foreach($arr_delete as $a){
	    			//拿到了文章ID
	    			
	    			$data=$pic->create();
	    			$data["pic_status"]=1;
	    			$result=$pic->where("id=$a")->save($data);
	    			
		    		if($result !== false){
				        $n++;
				    }
	    		}
		    	
		    	
		    	$this->success("删除图片  $n 个！", U('Admin/Pic/PicList', '', false, true));
	    	}else{
	    		$this->success("$arr_delete", U('Admin/Pic/PicList', '', false, true));
	    	}
    	}
    	
    }
    
}